package entities;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.persistence.*;


/**
 * The persistent class for the User database table.
 * 
 */
@Entity
public class User implements Serializable {
        private static final long serialVersionUID = 1L;

        @Id
        private String user;

        private int age;

        private String mail;

        private String name;

        private String pass;

    @Lob()
        private byte[] photo;

        private String sex;

        private String surname;

        private byte validated;
        
     // bi-directional many-to-one association to Event
        @OneToMany(mappedBy = "promoters",fetch=FetchType.EAGER)
        private Set<Event> orgaEvents=new HashSet<Event>();
        
     // bi-directional many-to-many association to Event
        @ManyToMany(cascade = CascadeType.REFRESH,fetch=FetchType.EAGER, mappedBy = "participants")
        private List<Event> parEvents=new ArrayList<Event>();
        
        
        /**
         * Un user a plusieurs comments
         */
        @OneToMany(mappedBy = "user", fetch = FetchType.LAZY, cascade = CascadeType.REMOVE)
        private Set<Comment> comments;
        
		public List<Event> getParEvents() {
			return parEvents;
		}

		public void setParEvents(List<Event> parEvents) {
			this.parEvents = parEvents;
		}

		public User() {
        }

        public String getUser() {
                return this.user;
        }

        public void setUser(String user) {
                this.user = user;
        }

        public int getAge() {
                return this.age;
        }

        public void setAge(int age) {
                this.age = age;
        }

        public String getMail() {
                return this.mail;
        }

        public void setMail(String mail) {
                this.mail = mail;
        }

        public String getName() {
                return this.name;
        }

        public void setName(String name) {
                this.name = name;
        }

        public String getPass() {
                return this.pass;
        }

        public void setPass(String pass) {
                this.pass = pass;
        }

        public byte[] getPhoto() {
                return this.photo;
        }

        public void setPhoto(byte[] photo) {
                this.photo = photo;
        }

        public String getSex() {
                return this.sex;
        }

        public void setSex(String sex) {
                this.sex = sex;
        }

        public String getSurname() {
                return this.surname;
        }

        public void setSurname(String surname) {
                this.surname = surname;
        }

        public byte getValidated() {
                return this.validated;
        }

        public void setValidated(byte validated) {

                this.validated = validated;
        }

		public Set<Event> getEvents() {
			return orgaEvents;
		}

		public void setEvents(Set<Event> events) {
			this.orgaEvents = events;
		}
        
    	public void proEvent(Event nEvent){
    		nEvent.setPromoters(this);
    		this.orgaEvents.add(nEvent);
	}
    	public void parEvent(Event nEvent){
    		this.parEvents.add(nEvent);
    	}
    	public void delParEvent(Event nEvent){
           // if(this.parEvents.contains(nEvent)){  //凭什么判断teacher在集合teachers中呢？是根据teacher的id。这就要求必要重写Teacher.java的hasCode和equals方法，通过这两个方法来判断对象是否相等。  
           //     this.parEvents.remove(nEvent);  
           // }
    		/*Iterator trol = parEvents.iterator(); //To seek and destroy!
        	while (trol.hasNext()) {
        		if(nEvent.toString().equals(trol.next().toString())){
        			this.parEvents.remove(nEvent);  
        		}
        	}*/
    		boolean found=false;
        	int i=0;
        	do{
        		if(parEvents.get(i).getIdEvent() == nEvent.getIdEvent()) {
        			found=true;
        		}
        		else
        			i++;
        	}while(i<parEvents.size() && !found);
        	parEvents.remove(i);
    		
    	}
    	
	    public Set<Comment> getComments() {
	        return comments;
	    }

	    public void setComments(Set<Comment> comments) {
	        this.comments = comments;
	    }
	    
	    public void addComment(Comment comment) {
	        this.comments.add(comment);
	    }


        @Override  
        public int hashCode() {  
            final int prime = 31;  
            int result = 1;  
            result = (int) (prime * result +System.currentTimeMillis());  
            //判断的依据是，如果id不为null的话，就返回id的哈希码。  
            return result;  
        }  
        @Override  
        public boolean equals(Object obj) {  
        	boolean flag=false;
        	final User other = (User) obj; 
            if (this == obj)  
                flag= true;    
             
            if(user.equals(other.getUser()))  
            	 flag= true;  
            return flag;  
        } 
}